package com.hertz.appr.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hertz.appr.http.MyHttpClient;
import com.hertz.appr.http.MyHttpElement;
import com.hertz.appr.http.Parameter;
import com.hertz.appr.http.Response;
import com.hertz.appr.json.JSONArray;
import com.hertz.appr.json.JSONException;
import com.hertz.appr.json.JSONObject;
import com.hertz.appr.oauth.AccessToken;
import com.hertz.appr.oauth.OAuth;
import com.hertz.appr.oauth.OAuthToken;
import com.hertz.appr.pojo.db.Message;
import com.hertz.appr.pojo.db.Robot;
import com.hertz.appr.util.Configuration;
import com.hertz.appr.util.WeiboException;

public class FriendShipService {

	private Logger log = Logger.getLogger(FriendShipService.class);
//	private String provider;
	private MyHttpClient sinaHttpClient;
	private MyHttpClient qqHttpClient;
	private final String qq_user_idolist = "http://open.t.qq.com/api/friends/user_idollist";
	private RobotGang robotGang;
	public FriendShipService(){
		sinaHttpClient = new MyHttpClient(Configuration.SINA_PROVIDER);
		qqHttpClient = new MyHttpClient(Configuration.QQ_PROVIDER);
		
	}
	
	public boolean isFollowing(String sourceUid,String targetUid,String provider) throws Exception{
		if(provider.equals(Configuration.QQ_PROVIDER)){
			return isFollowing(sourceUid, targetUid);
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			
		}
		return false;
	}
	private boolean isFollowing(String sourceUid,String targetUid)throws Exception{
		boolean result = false;
		List<Parameter> paramList = new ArrayList<Parameter>();
		Parameter param = new Parameter("name", sourceUid);
		paramList.add(param);
		paramList.add(new Parameter("reqnum", "30"));
		paramList.add(new Parameter("startindex", "0"));
		Integer start = 0;
		OAuth oauth = robotGang.getOauth(Configuration.QQ_PROVIDER, Robot.type_r,Robot.type_r);
		if(oauth == null){
			log.info("no oauth,may be api limit");
			return false;
		}
		while(true){
			paramList.remove(2);
			paramList.add(new Parameter("startindex", start.toString()));
			MyHttpElement he = oauth.getOauthHttpElement(qq_user_idolist,MyHttpClient.GET, paramList);
			log.debug("queryString : " + he.getQueryString());
			Response rs = qqHttpClient.httpRequest(qq_user_idolist, he);
			List<String> followingUsers = constructFriendListQQ(rs);
			for(String uid : followingUsers){
				if(uid.equals(targetUid)){
					log.info(sourceUid + " following " + targetUid + " is true");
					result = true;
					break;
				}
			}
			if(followingUsers.size() < 30){
				log.info("all following had fetch");
				break;
			}
			start += 30;
		}
		
		return result;
	}
	
	private List<String> constructFriendListQQ(Response res) throws Exception{
		log.debug(res.toString());
		List<String> list = new ArrayList<String>();
		if(res == null){
			return list;
		}
		try {
			JSONArray ja = null;
			JSONObject jsonObject = res.asJSONObject();
			if(jsonObject.getString("ret") == null || !jsonObject.getString("ret").equals("0")){
				log.debug("result is error,response=" + res);
				return null;
			}
			JSONObject data = jsonObject.getJSONObject("data");
			if(data.toString().equals("null")){
				log.debug("result is error,data=null");
				return null;
			}
			ja = jsonObject.getJSONObject("data").getJSONArray("info");
            int size = ja.length();
            list = new ArrayList<String>(size);
           
            for (int i = 0; i < size; i++) {
                list.add(ja.getJSONObject(i).getString("name"));
            }
            return list;
        } catch (JSONException jsone) {
            throw new WeiboException(jsone);
        } catch (WeiboException te) {
            throw te;
        }
	}
}
